//
//  GuideViewController.swift
//  OverseasOnlineShopping
//
//  Created by 唐先才 on 16/3/7.
//  Copyright © 2016年 唐先才. All rights reserved.
//

import UIKit

class GuideViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    
    private var collectionView: UICollectionView?
    private var imagesArray = ["guide1", "guide2", "guide3", "guide4"]
    private var isHiddenNextButton = true
    private var pageControl = UIPageControl(frame: CGRect(x: 0, y: HEIGHT - 50, width: WIDTH, height: 20))
    
    override func viewDidLoad() {
        super.viewDidLoad()
        createCollectionView()
        //        createPageControl()
    }
    
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    
    func createCollectionView() {
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView = UICollectionView(frame: UIScreen.mainScreen().bounds, collectionViewLayout: layout)
        guard let collectionView = self.collectionView else {
            print("创建collectionView失败")
            return
        }
        view.addSubview(collectionView)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.pagingEnabled = true
        collectionView.bounces = false
        let nib = UINib(nibName: "GuideCell", bundle: NSBundle.mainBundle())
        collectionView.registerNib(nib, forCellWithReuseIdentifier: "guideCell")
    }
    
    func createPageControl() {
        pageControl.numberOfPages = imagesArray.count
        pageControl.currentPage = 0
        view.addSubview(pageControl)
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imagesArray.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        guard let guideCell = collectionView.dequeueReusableCellWithReuseIdentifier("guideCell", forIndexPath: indexPath) as? GuideCell else {
            return UICollectionViewCell()
        }
        guideCell.guidePic.image = UIImage(named: imagesArray[indexPath.row])
        if indexPath.row == 0 || indexPath.row == 1 || indexPath.row == 2 {
            guideCell.setNextBtnHidden(true)
        } else {
            guideCell.setNextBtnHidden(false)
        }
        return guideCell
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsZero
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSizeMake(WIDTH, HEIGHT)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}

